package fallen.deck;

import fallen.deck.front_end.MainWindow;
import fallen.deck.front_end.SplashScreen;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.hibernate.HibernateException;
import sun.tools.jar.Main;

/**
 * App launcher!
 *
 */
public class App {

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                try {

                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

                } catch (InstantiationException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                } catch (UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                }
                try{
                JarFile jf = findJarfile(Main.class);
                if (jf != null) {
                    Manifest manifest = jf.getManifest();
                    Map<String, Attributes> entries = manifest.getEntries();
                    System.out.println("Entries: " + entries.size() + "\n"
                            + entries);
                }
                }catch(IOException e){e.printStackTrace();}
                new Thread() {

                    public void run() {
                        SplashScreen sp = new SplashScreen();
                        try {
                            sp.setVisible(true);
                            //Thread.sleep(1500);
                            //if (!Queries.areCardsLoaded())
                            if (false) {
                                JOptionPane.showMessageDialog(null,
                                        "No se encontraron cartas en la base de datos."
                                        + "Si el problema persiste, \ndescarga de nuevo"
                                        + "la aplicación.", "Initialization Problem", JOptionPane.ERROR_MESSAGE);
                            }
                            Drecker.setWindow(new MainWindow());
                            Drecker.getWindow().setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH); 
                            Drecker.getWindow().setVisible(true);
                            //Thread.sleep(500);
                        } catch (HibernateException he) {
                            JOptionPane.showMessageDialog(null,
                                    "No se pudo iniciar la base de datos."
                                    + "Si el problema persiste, \ndescarga de nuevo"
                                    + "la aplicación.", "Initialization Problem", JOptionPane.ERROR_MESSAGE);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        } finally {
                            sp.dispose();
                        }
                    }
                }.start();

            }
        });
    }

    private static JarFile findJarfile(Class<?> c) throws IOException {
        String classname = c.getName();
        classname = "/" + classname.replaceAll("\\.", "/") + ".class";
        URL url = c.getResource(classname);
//System.out.println( "URL = " + url );
        String urlString = url.toString();
        if (urlString.startsWith("jar:")) {
            String filename = urlString.substring(9,
                    urlString.indexOf('!'));
            JarFile jarfile = new JarFile(filename);
            return jarfile;
// Manifest manifest = jarfile.getManifest();
// Map<String, Attributes> entries = manifest.getEntries();
// System.out.println( "Entries:\n"+ entries );
        } else {
            return null;
// System.out.println( "Class is not associated with a JAR." );
        }
    }
}
